<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row v-if="title === '备注'">
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" rows="4" :maxLength="50" placeholder="请输入备注(最多50字)" />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row v-else>
          <a-col :span="24">
            <a-form-model-item label="物流商" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <j-dict-select-tag type="list" v-model="model.type" dictCode="logistics_provider_type" placeholder="请选择物流商" :disabled="title === '编辑'" @change="typeChange" />
            </a-form-model-item>
          </a-col>
          <a-divider/>
          <span class="tag">自定义信息</span>
          <a-col :span="24">
            <a-form-model-item label="物流商代码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code" :rules="[{ required: true,message: '请输入物流商代码' }, { message: '请输入英文与数字', pattern: /^[a-zA-Z0-9]+$/ }]">
              <a-input v-model="model.code" placeholder="请输入物流商代码" :disabled="title === '编辑'" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="物流商名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入物流商名称(建议使用中文)"  ></a-input>
              <div class="has-error">
                <div class="ant-form-explain" v-if="validateMessage">{{ validateMessage }}</div>
              </div>
            </a-form-model-item>
          </a-col>
          <a-divider v-if="model.type"/>
          <span class="tag tag_" v-if="model.type">物流商提供</span>
          <a-col :span="24" v-if="model.type == 'YUN_TU'">
            <a-form-model-item label="客户代码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="configJson.apiKey" :rules="[{ required: true, message: '请输入客户代码' }]">
              <a-input v-model="model.configJson.apiKey" placeholder="请输入客户代码" ></a-input>
            </a-form-model-item>
            <a-form-model-item label="Apisecret" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="configJson.apiSecret" :rules="[{ required: true, message: '请输入Apisecret' }]">
              <a-input v-model="model.configJson.apiSecret" placeholder="请输入Apisecret" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="model.type == 'YAN_WEN'">
            <a-form-model-item label="客户代码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="configJson.apiKey" :rules="[{ required: true, message: '请输入客户代码' }]">
              <a-input v-model="model.configJson.apiKey" placeholder="请输入客户代码" ></a-input>
            </a-form-model-item>
            <a-form-model-item label="Token" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="configJson.apiSecret" :rules="[{ required: true, message: '请输入Token' }]">
              <a-input v-model="model.configJson.apiSecret" placeholder="请输入Token" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="model.type == 'DI_YI'">
            <a-form-model-item label="API账号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="configJson.apiKey" :rules="[{ required: true, message: '请输入API账号' }]">
              <a-input v-model="model.configJson.apiKey" placeholder="请输入API账号" ></a-input>
            </a-form-model-item>
            <a-form-model-item label="API秘钥" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="configJson.apiSecret" :rules="[{ required: true, message: '请输入API秘钥' }]">
              <a-input v-model="model.configJson.apiSecret" placeholder="请输入API秘钥" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="model.type == 'TONG_YOU'">
            <a-form-model-item label="API地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="configJson.apiUrl" :rules="[{ required: true, message: '请输入API地址' }]">
              <a-input v-model="model.configJson.apiUrl" placeholder="请输入API地址" ></a-input>
            </a-form-model-item>
            <a-form-model-item label="拓扑链授权Token" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="configJson.apiKey" :rules="[{ required: true, message: '请输入授权Token' }]">
              <a-input v-model="model.configJson.apiKey" placeholder="请输入拓扑链授权Token" ></a-input>
            </a-form-model-item>
            <a-form-model-item label="轨迹授权Token" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="configJson.trackKey" :rules="[{ required: true, message: '请输入授权trackKey' }]">
              <a-input v-model="model.configJson.trackKey" placeholder="请输入轨迹授权Token" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="model.type == 'HUA_HAN'">
            <a-form-model-item label="API地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="configJson.apiUrl" :rules="[{ required: true, message: '请输入API地址' }]">
              <a-input v-model="model.configJson.apiUrl" placeholder="请输入API地址" ></a-input>
            </a-form-model-item>
            <a-form-model-item label="Token" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="configJson.apiKey" :rules="[{ required: true, message: '请输入Token' }]">
              <a-input v-model="model.configJson.apiKey" placeholder="请输入Token" ></a-input>
            </a-form-model-item>
            <a-form-model-item label="API秘钥" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="configJson.apiSecret" :rules="[{ required: true, message: '请输入API秘钥' }]">
              <a-input v-model="model.configJson.apiSecret" placeholder="请输入API秘钥" ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'LogisticsProviderForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      },
      title: {
        type: String,
        default: ''
      }
    },
    data () {
      return {
        model:{
          configJson:{}
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          remark: [{ required: true, message: '请输入备注' }],
          type: [{ required: true, message: '请选择物流商' }],
          name: [{ required: true, message: '请输入物流商名称' }],
        },
        url: {
          add: "/logistics/logisticsProvider/add",
          edit: "/logistics/logisticsProvider/edit",
          queryById: "/logistics/logisticsProvider/queryById"
        },
        validateMessage: ''
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    watch: {
      'model.name': {
        handler(val){
          this.validateMessage = '';
        }
      }
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                if(res.message.indexOf('该物流商名称已经存在不能重复创建！') > -1){
                  that.validateMessage = res.message;
                }else{
                  that.$message.warning(res.message);
                }
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      typeChange(){
        //物流商切换
        this.model.configJson = {};
      }
    }
  }
</script>

<style lang="less" scoped>
.tag{
  border: 1px solid #2dd2aa;
  padding: 2px 10px;
  border-radius: 6px;
  color: #2dd2aa;
  margin-bottom: 4px;
  display: inline-block;
}
.tag_{
  border: 1px solid #1890FF;
  color: #1890FF;
}
</style>